package com.gitee.elf.api.nms

import org.bukkit.Location
import org.bukkit.event.entity.CreatureSpawnEvent
import taboolib.library.reflex.Reflex.Companion.getProperty
import taboolib.library.reflex.Reflex.Companion.invokeMethod
import taboolib.module.nms.MinecraftVersion
import taboolib.module.nms.nmsClass

const val DEFAULT_VERSION = "v1_12_R1"

inline fun <reified T> Location.nmsWorld(): T {
    return this.world!!.getProperty<T>("world")!!
}

var nmsEntityId = 54

val nmsMinecraftKey = nmsClass("MinecraftKey")

val nmsEntityTypes = nmsClass("EntityTypes")

fun Location.addEntity(any: Any) {
    this.world?.invokeMethod<Void>("addEntity", any, CreatureSpawnEvent.SpawnReason.CUSTOM)
}

@Suppress("UNCHECKED_CAST")
fun nmsEntityTypeManager(): Class<EntityTypeManager> {
    return try {
        Class.forName("com.gitee.elf.api.nms.${MinecraftVersion.minecraftVersion}.EntityTypeManager") as Class<EntityTypeManager>
//        BukkitEntityTypeManager::class.java as Class<EntityTypeManager>
    } catch (e: ClassNotFoundException) {
        BukkitEntityTypeManager::class.java as Class<EntityTypeManager>
    }
}